<?php
   // ************************************************************************
   // **                                                                    **
   // **          This original software was designed and coded by          **
   // **                                                                    **
   // **                         David A. Goldsmith                         **
   // **                                                                    **
   // **                       dave106@wonderdave.com                       **
   // **                                                                    **
   // **                          March/April 2005                          **
   // **            Revised for Galileo High School, August 2009            **
   // **                                                                    **
   // ************************************************************************
?>


<html>
<title>Computer Lab Signup System Administration</title>

<link rel="stylesheet" href="LAB-Admin.css" type="text/css">

<center>
   <font color="#770000" size=5><b><u>Computer Lab Signup System Administration</u></b></font><br><br>
   <font color="#800080" size=4><b>Add / Edit / Delete Daily Schedules</b></font>
</center>
<br><br><br>


<script language="JavaScript" type="text/javascript">
   function ConfirmDelete()
   {
      return confirm('Are you sure that you want to permanently\n' +
                     'REMOVE the lab daily schedule shown here?\n\n' +
                     '( OK = Yes    Cancel = No )');
   }
</script>


<?php
   include("Configure.php");


   // In case the "register_globals" PHP directive is off, make sure all of the passed form variables are accessible
   $DeleteDailySchedule = $_POST['DeleteDailySchedule'];
   $Info1               = $_POST['Info1'];
   $Info2               = $_POST['Info2'];
   $Info3               = $_POST['Info3'];
   $Info4               = $_POST['Info4'];
   $Info5               = $_POST['Info5'];
   $Info6               = $_POST['Info6'];
   $Info7               = $_POST['Info7'];
   $Info8               = $_POST['Info8'];
   $NewSchedule         = $_POST['NewSchedule'];
   $Schedule            = $_POST['Schedule'];
   $Submit              = $_POST['Submit'];


   function StripBadChars($String)
   {
      $BadChars = array("\"", "\'", "\\");
      $String = str_replace($BadChars, '', $String);

      return $String;
   }


   if ($NewSchedule)
      $Schedule = $NewSchedule;


   // Strip away "bad" characters from the form variables that may have just been submitted
   $Schedule = StripBadChars($Schedule);
   $Info1 = StripBadChars($Info1);
   $Info2 = StripBadChars($Info2);
   $Info3 = StripBadChars($Info3);
   $Info4 = StripBadChars($Info4);
   $Info5 = StripBadChars($Info5);
   $Info6 = StripBadChars($Info6);
   $Info7 = StripBadChars($Info7);
   $Info8 = StripBadChars($Info8);


   // See if a lab daily schedule description has been entered
   if (($Schedule) and ($Schedule != "No Classes"))
   {
      // Determine if the lab daily schedule description already exists in the "LabDayTypes" table
      $Query1 = mysql_query("SELECT Info1, Info2, Info3, Info4, Info5, Info6, Info7, Info8
                             FROM LabDayTypes WHERE Schedule='$Schedule'", $DB);
      $Data1 = mysql_fetch_array($Query1);
      $NumRecords1 = mysql_num_rows($Query1);


      // Determine if the daily schedule description exists in any of the "LabSignups" tables for this week or a future week
      $Query2 = mysql_query("SELECT Room FROM Labs", $DB);

      // Determine the date of the Monday for this current week (or for the upcoming week, if it is now a weekend)
      $ThisFriday = date("Y-m-d 00:00:00", strtotime("this Friday"));
      $Date = date("Y-m-d", strtotime("last Monday", strtotime($ThisFriday)));

      $NumRecords2 = 0;
      while ($Data2 = mysql_fetch_array($Query2))
      {
         $Room = $Data2["Room"];
         $Query3 = mysql_query("SELECT Date FROM LabSignups$Room
                                WHERE (Date>='$Date') AND (Schedule='$Schedule')", $DB);
         $NumRecords2 += mysql_num_rows($Query3);
      }
   }


   // See if a submission has been made of lab daily schedule information to be added/edited/deleted
   if ($Submit)
   {
      $Invalid = 0;

      // See if the lab daily schedule information is invalid
      $Info[1] = $Info1;
      $Info[2] = $Info2;
      $Info[3] = $Info3;
      $Info[4] = $Info4;
      $Info[5] = $Info5;
      $Info[6] = $Info6;
      $Info[7] = $Info7;
      $Info[8] = $Info8;
      $NumBlankPeriods = 0;

      for ($Counter=1; $Counter<=8; $Counter++)
      {
         if (($NumBlankPeriods > 0) and ($Info[$Counter]))
            $Invalid = 2;
         if (! $Info[$Counter])
            $NumBlankPeriods++;
      }

      if ($NumBlankPeriods > 7)
         $Invalid = 1;

      if ($DeleteDailySchedule)
         $Invalid = 0;

      if ($Invalid != 0)
         // The lab daily schedule information is NOT valid
         $Submit = "";

      if ($Invalid == 0)
      {
         // The lab daily schedule information IS valid; add/update/delete the information to/in/from the "LabDayTypes" table

         if ($NumRecords1 == 0)
            $Query = mysql_query("INSERT INTO LabDayTypes (Schedule, Info1, Info2, Info3, Info4,
                                                                     Info5, Info6, Info7, Info8)
                                  VALUES ('$Schedule', '$Info1', '$Info2', '$Info3', '$Info4',
                                                       '$Info5', '$Info6', '$Info7', '$Info8')", $DB);

         if (($NumRecords1 > 0) and (! $DeleteDailySchedule))
            $Query = mysql_query("UPDATE LabDayTypes SET Info1='$Info1', Info2='$Info2', Info3='$Info3', Info4='$Info4',
                                                         Info5='$Info5', Info6='$Info6', Info7='$Info7', Info8='$Info8'
                                  WHERE Schedule='$Schedule'", $DB);

         if ($DeleteDailySchedule)
            $Query = mysql_query("DELETE FROM LabDayTypes WHERE Schedule='$Schedule' LIMIT 1", $DB);


         // Allow another lab daily schedule to be added/edited/deleted
         echo "<script language='JavaScript'>alert('Database Updated');</script>";
         echo "<body onLoad=\"location.replace('EditDay.php')\">";
      }
   }


   if ((! $Schedule) or ($Schedule == "No Classes"))
   {
      // A lab daily schedule description has NOT been entered; set the focus to the appropriate object
      echo "<body onLoad='document.ContinueForm.NewSchedule.focus()'>";

      echo "<table align=center border=0 cellpadding=0 cellspacing=0>";
      echo "<form name='ContinueForm' method='post' action='EditDay.php'>";


      // Retrieve the descriptions of the lab daily schedules from the "LabDayTypes" table
      $Query = mysql_query("SELECT Schedule FROM LabDayTypes WHERE Schedule!='No Classes' ORDER BY Schedule", $DB);

      // Display a pulldown menu of all of the daily schedule descriptions in the "LabDayTypes" table
      echo "<td valign=top align=center><font face='Arial' size=3 color='#000000'>";
      echo "<b>Choose an existing schedule:</b></font><br><br>";

      echo "<select name='Schedule'>";
      echo "<option selected>";
      while ($Data = mysql_fetch_array($Query))
      {
         $TempSchedule = $Data["Schedule"];
         echo "<option>$TempSchedule";
      }
      echo "</select></td></center><br>";


      echo "<td valign=top align=center width=100><b><font face='Arial' size=3 color='#c00000'>OR</font></b></td>";


      echo "<td valign=top align=center height=120><b><font face='Arial' size=3 color='#000000'>";
      echo "Describe a new schedule:</font></b><br><br>";
      echo "<input type='text' name='NewSchedule' size=18 maxlength=18></td></tr>";


      echo "<tr><td colspan=3 align=center><input type='submit' name='Continue' value='Continue' ";
      echo "style='width:80px;font-weight:bold;color:#006000'> &nbsp; &nbsp; &nbsp; &nbsp; ";

      // Display a button that returns to the Main Menu
      echo "<input type='button' value='Return to Main Menu' ";
      echo "style='width:154px;font-weight:bold;color:#000090' ";
      echo "onClick=\"location.replace('AdminMenu.php')\">";
      echo "</td></tr>";
      echo "</form></table>";
   }


   if (($Schedule) and ($Schedule != "No Classes") and (! $Submit))
   {
      // A lab daily schedule description HAS been entered; set the focus to the appropriate object
      echo "<body onLoad='document.PeriodForm.Info1.focus()'>";


      echo "<center><form name='PeriodForm' method='post' action='EditDay.php'>";
      echo "<input type='hidden' name='Schedule' value='$Schedule'>";

      echo "<b><font face='Arial' size=3 color='#000000'>";
      echo "Lab Daily Schedule:</font></b> &nbsp; &nbsp;";
      echo "<font size=4 color='#000090'><b>$Schedule</b></font><br><br><br>";


      echo "<table align=center border=3 cellpadding=3 cellspacing=0 bordercolor='#800080'>";
      echo "<tr valign=top bgcolor='#000000'>";
      echo "<th width=240><font color='#ffffff'>Current Period Description</font></th>";
      echo "<th width=240><font color='#ffffff'>New Period Description</font></th>";
      echo "</tr>";


      if (! $Info1) { $Info1 = $Data1["Info1"]; }
      if (! $Info2) { $Info2 = $Data1["Info2"]; }
      if (! $Info3) { $Info3 = $Data1["Info3"]; }
      if (! $Info4) { $Info4 = $Data1["Info4"]; }
      if (! $Info5) { $Info5 = $Data1["Info5"]; }
      if (! $Info6) { $Info6 = $Data1["Info6"]; }
      if (! $Info7) { $Info7 = $Data1["Info7"]; }
      if (! $Info8) { $Info8 = $Data1["Info8"]; }

      for ($Counter=1; $Counter<=8; $Counter++)
      {
         // Display the CURRENT period description
         $TempInfo = $Data1["Info$Counter"];
         if ($TempInfo)
            echo "<tr><td>$TempInfo</td>";
         else
         {
            echo "<tr><td> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ";
            echo "-----&nbsp; No Description &nbsp;-----</td>";
         }

         // Allow the user to modify the current period description
         if ($Counter == 1)
            echo "<td align=center><input type='text' name='Info1' value='$Info1' size=25 maxlength=25></td></tr>";
         if ($Counter == 2)
            echo "<td align=center><input type='text' name='Info2' value='$Info2' size=25 maxlength=25></td></tr>";
         if ($Counter == 3)
            echo "<td align=center><input type='text' name='Info3' value='$Info3' size=25 maxlength=25></td></tr>";
         if ($Counter == 4)
            echo "<td align=center><input type='text' name='Info4' value='$Info4' size=25 maxlength=25></td></tr>";
         if ($Counter == 5)
            echo "<td align=center><input type='text' name='Info5' value='$Info5' size=25 maxlength=25></td></tr>";
         if ($Counter == 6)
            echo "<td align=center><input type='text' name='Info6' value='$Info6' size=25 maxlength=25></td></tr>";
         if ($Counter == 7)
            echo "<td align=center><input type='text' name='Info7' value='$Info7' size=25 maxlength=25></td></tr>";
         if ($Counter == 8)
            echo "<td align=center><input type='text' name='Info8' value='$Info8' size=25 maxlength=25></td></tr>";
      }

      echo "</table><br>";


      echo "<table align=center border=0 cellpadding=0 cellspacing=0><tr><td valign=top height=50>";

      // Determine whether or not the current lab daily schedule can be removed; the daily schedule can only be deleted
      //  if the schedule is not presently being used in the current week or in any future weeks
      if (($NumRecords1 > 0) and ($NumRecords2 == 0))
      {
         echo "<input type='checkbox' name='DeleteDailySchedule' ";
         echo "onClick='if (checked) { return ConfirmDelete() }'>";
         echo "<b>Delete Daily Schedule</b></td></tr>";
      }
      else
         echo "<br><br><br>";

      echo "</td></tr></table>";


      // Display a button that saves the added/edited/deleted teacher information
      echo "<input type='submit' name='Submit' value='Save Information' ";
      echo "style='width:130px;font-weight:bold;color:#006000'> &nbsp; &nbsp; &nbsp; &nbsp; ";

      // Display a button that cancels any unsaved changes and restarts the add/edit/delete teacher information process
      echo "<input type='button' value='Cancel' ";
      echo "style='width:80px;font-weight:bold;color:#c00000' ";
      echo "onClick=\"location.replace('EditDay.php')\"> &nbsp; &nbsp; &nbsp; &nbsp; ";

      // Display a button that returns to the main menu (without saving any changes)
      echo "<input type='button' value='Return to Main Menu' ";
      echo "style='width:154px;font-weight:bold;color:#000090' ";
      echo "onClick=\"location.replace('AdminMenu.php')\">";
      echo "</form></center>";


      if ($Invalid == 1)
      {
         echo "<center><font face='Arial' size=5 color='#c00000'><b>";
         echo "Too Few Lab Periods Used<br>";
         echo "</b></font></center>";
      }

      if ($Invalid == 2)
      {
         echo "<center><font face='Arial' size=5 color='#c00000'><b>";
         echo "Empty Periods Must Be Last<br>";
         echo "</b></font></center>";
      }
   }
?>


</body>

</html>
